package com.module03.work.demo04;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

public class ListDatabase {

    //创建List保存学生信息
    static List<Student> stuList = new ArrayList<>();

    static {
        //初始化集合
        stuList.add(new Student(1, "张飞", 30));
        stuList.add(new Student(2, "关羽", 35));
        stuList.add(new Student(3, "刘备", 40));
    }

    static Scanner sc = new Scanner(System.in);


    //增加学生信息方法，
    public static void add() {

        boolean b1 = true;
        while (b1) {
            System.out.println("请输入待增加学生的学号、姓名、年龄信息,输入-1返回上一层");
            int id = sc.nextInt();
            if (id == -1) {
                break;
            }
            String name = sc.next();
            int age = sc.nextInt();
            //每次添加都开辟新的储存空间
            Student st = new Student(id, name, age);
            if (stuList.contains(st) || id < 0 || id > 10000) {
                System.out.println("学号已存在，请重新输入");
                add();
            } else if (age < 0 || age > 150) {
                System.out.println("年龄不合理哦！");
                add();
            } else {
                stuList.add(st);
                System.out.println(stuList.toString());
                System.out.println("========>>添加成功<<========");
            }

            //是否继续添加
            System.out.println("是否继续添加 ？");
            String choose = sc.next();
            switch (choose) {
                case "是":
                    break;
                default:
                    b1 = false;
            }
        }

    }

    //删除学生信息方法，
    public static void delete() {

        boolean b1 = true;
        while (b1) {
            System.out.println("请输入待删除学生的学号,输入-1返回上一层");
            int id = sc.nextInt();
            if (id == -1) {
                break;
            }
            Student st = new Student(id);
            if (!stuList.contains(st)) {
                System.out.println("找不到学号，请重新输入");
                delete();
            } else {
                stuList.remove(st);
                System.out.println(stuList.toString());
                System.out.println("========>>删除成功<<========");
            }
            //是否继续删除
            System.out.println("是否继续删除 ？");
            String choose = sc.next();
            switch (choose) {
                case "是":
                    break;
                default:
                    b1 = false;
            }
        }
    }

    //修改学生信息方法，
    public static void update() {
        boolean b1 = true;
        while (b1) {
            System.out.println("请输入待修改的学生的学号和修改后的姓名、年龄信息,输入-1返回上一层");
            int id = sc.nextInt();
            if (id == -1) {
                break;
            }
            String name = sc.next();
            int age = sc.nextInt();
            Student st = new Student(id, name, age);
            if (!stuList.contains(st)) {
                System.out.println("找不到学号，请重新输入！");
                update();
            } else if (age < 0 || age > 150) {
                System.out.println("年龄不合理哦！请重新输入！");
                update();
            } else {
                int index = stuList.indexOf(st);
                stuList.set(index, st);
                System.out.println(stuList.toString());
                System.out.println("========>>修改成功<<========");
            }
            //是否继续修改
            System.out.println("是否继续修改 ？");
            String choose = sc.next();
            switch (choose) {
                case "是":
                    break;
                default:
                    b1 = false;
            }
        }
    }

    //查询指定学生信息方法，通过学号、名字、年龄
    public static void query() {
        System.out.println("请输入查询的学生的学号");
        int id = sc.nextInt();
        Student st = new Student(id);
        if (!stuList.contains(st)) {
            System.out.println("找不到学号，请重新输入");
            query();
        } else {
            int index  = stuList.indexOf(st);
            System.out.println(stuList.get(index));
            System.out.println("========>>查询成功<<========");
        }

        //是否继续查询
        System.out.println("是否继续查询 ？");
        String choose = sc.next();
        switch (choose) {
            case "是":
                query();
                break;
            default:
        }
   }

    //查询全部学生信息方法
    public static void queryAll() {
        System.out.println(stuList.toString());
        System.out.println("========>>查询成功<<========");
    }


}
